package com.flute.haflute.common;

import java.net.InetAddress;
import java.util.Map;
import java.util.Properties;

import com.flute.haflute.monitor.Cpu;

/**
 * 集群上下环境变量，供集群所有JVM类型使用
 * 
 */
public class ClusterContext {
	private static String dM = "localhost";
	private static String bDM = "localhost";
	private static InetAddress localAddress;

	/** 平台运行的根目录 */
	private static String rootDir = "";

	private static int rmiPort = 1099;
	private static int heartBeatPort = 2012;
	private static int heartBeatInterval = 60 * 1000;
	private static int heartBeatTries = 3;
	private static int heartBeatFailures = 3;

	private static boolean jvmDebugging = false;
	private static Properties clusterProps = new Properties();
	// TODO need to calculate from cpu,mem or disk
	private static long clusterId = System.currentTimeMillis();

	private static int cpucores = -1;
	private static int monitorMsgAge = 10;
	private static int fileSize = 200;
	private static Map<String, String> topics = null;

	public static String getDM() {
		return dM;
	}

	public static void setDM(String dm) {
		dM = dm;
	}

	public static String getBDM() {
		return bDM;
	}

	public static void setBDM(String bdm) {
		bDM = bdm;
	}

	public static int getRMIPort() {
		return rmiPort;
	}

	public static void setRMIPort(int port) {
		rmiPort = port;
	}

	public static int getHeartBeatPort() {
		return heartBeatPort;
	}

	public static void setHeartBeatPort(int heartBeatPort) {
		ClusterContext.heartBeatPort = heartBeatPort;
	}

	/** 获取分布式平台的根目录 */
	public static String getRootDir() {
		if ("".equals(rootDir)) {
			// rootDir = System.getProperty("user.dir");
			rootDir = ClusterContext.class.getResource("/").getPath()
					.toString();
			/*
			 * rootDir = (new StringBuilder(rootDir)).append(File.separator)
			 * .toString();
			 */
		}
		return rootDir;
	}

	public static void setRootDir(String dir) {
		rootDir = dir;
	}

	public static String getClusterProperty(String item) {
		return clusterProps.getProperty(item);
	}

	public static void setClusterProperty(String item, String value) {
		clusterProps.setProperty(item, value);
	}

	/**
	 * @return 以毫秒为单位的心跳间隔
	 */
	public static int getHeartBeatInterval() {
		return heartBeatInterval;
	}

	/**
	 * @param heartBeatInterval
	 *            设置以毫秒为单位的心跳间隔
	 */
	public static void setHeartBeatInterval(int heartBeatInterval) {
		ClusterContext.heartBeatInterval = heartBeatInterval;
	}

	/**
	 * @return 每轮心跳尝试的最多次数
	 */
	public static int getHeartBeatTries() {
		return heartBeatTries;
	}

	/**
	 * @param heartBeatTries
	 *            每轮心跳尝试的最多次数
	 */
	public static void setHeartBeatTries(int heartBeatTries) {
		ClusterContext.heartBeatTries = heartBeatTries;
	}

	/**
	 * @return 节点失效前心跳丢失的次数
	 */
	public static int getHeartBeatFailures() {
		return heartBeatFailures;
	}

	/**
	 * @param heartBeatFailures
	 *            设置节点失效前心跳丢失的次数
	 */
	public static void setHeartBeatFailures(int heartBeatFailures) {
		ClusterContext.heartBeatFailures = heartBeatFailures;
	}

	/** @return 本节点的地址 */
	public static InetAddress getLocalAddress() {
		return localAddress;
	}

	/** @param 设置本节点的地址 */
	public static void setLocalAddress(InetAddress localAddress) {
		ClusterContext.localAddress = localAddress;
	}

	/** 获取本节点在集群的唯一ID */
	public static long getClusterId() {
		return clusterId;
	}

	public static int getCpucores() {
		if (cpucores < 0) {
			cpucores = new Cpu().getTotalCpuCores(cpucores);
		}
		return cpucores;
	}

	public static void setCpucores(int cpucores) {
		ClusterContext.cpucores = cpucores;
	}

	/**
	 * @param jvmDebugging
	 *            the jvmDebugging to set
	 */
	public static void setJvmDebugging(boolean jvmDebugging) {
		ClusterContext.jvmDebugging = jvmDebugging;
	}

	public static boolean isJvmDebugging() {
		return jvmDebugging;
	}

	public static int getFileSize() {
		return fileSize;
	}

	public static void setFileSize(int fileSize) {
		ClusterContext.fileSize = fileSize;
	}

	public static int getMonitorMsgAge() {
		return monitorMsgAge;
	}

	public static void setMonitorMsgAge(int monitorMsgAge) {
		ClusterContext.monitorMsgAge = monitorMsgAge;
	}

	public static Map<String, String> getTopics() {
		return topics;
	}

	public static void setTopics(Map<String, String> topics) {
		ClusterContext.topics = topics;
	}

}
